import time

import appium
from appium.webdriver.common.appiumby import AppiumBy
from Util.BasePage import BasePage
from appium import webdriver
from Util.AppDriver import *


class MyslPage(BasePage):

    # findEnergy = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("")')
    qubaohuTxt0 = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("去保护")')
    qubaohuTxt1 = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("去保护")')
    qubaohu = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("去保护")')
    nowEner = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("g")')
    nowEner2 = 'new UiSelector().textContains("g")'
    nishouquTA = (AppiumBy.XPATH, '//android.widget.TextView[@textContains="你收取TA"]')  # Xpath不能用textContains
    seeAll = (AppiumBy.XPATH, '//android.widget.TextView[@text="去看全部"]')
    nishouquxpath = (AppiumBy.XPATH, '//android.widget.TextView[@textContains="g"]')
    todayAll = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("今日共获得")')
    nishouquTA4 = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("你收取TA")')
    myslAll = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("去看全部")')
    myslIcon = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("蚂蚁森林")')
    myzyIcon = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("蚂蚁庄园")')

    # self.driver.find_element_by_android_uiautomator('new UiSelector().text("{}")'.format(i)).click()
    # self.driver.find_elements_by_android_uiautomator('new UiSelector().textContains("收集能量")')


    def todayEnergy(self):
        # self.driver.swipe(888, 1090, 222, 1090, 500)
        # print(self.driver.page_source)
        #
        # start_x = int(self.driver.get_window_size()['width']/2)
        # start_y = self.driver.get_window_size()['height']-100
        # end_x = int(self.driver.get_window_size()['width']/2)
        # end_y = 100
        #
        # self.driver.swipe(start_x, start_y, end_x, end_y, 1000)
        # time.sleep(1)

        try:
            # print(self.driver.page_source)
            self.findelement(self.seeAll)
            a = self.findelement(self.seeAll).text
            print(a)
            self.click(self.seeAll) # qukanquanbu
            time.sleep(5)
            print(self.driver.page_source)
            self.findelement(self.todayAll)  # qukanquanbu
            # a = self.findelement(self.todayAll).text[5:-1] # qukanquanbu

            # print(a)

            a = int(self.findelement(self.todayAll).text[5:-1])  # qukanquanbu

            print(a)
            self.driver.back()
            time.sleep(1.5)

            # self.driver.swipe(end_x, end_y, start_x, start_y, 1000)

            print(a)
            return a

        except:

            self.findelement(self.myslAll)
            a = self.findelement(self.myslAll).text  # g
            print(a)

            self.click(self.myslAll)  # qukanquanbu

            time.sleep(2.5)
            print(self.driver.page_source)
            self.findelement(self.todayAll)  # qukanquanbu
            a = int(self.findelement(self.todayAll)[4:-1])  # qukanquanbu
            print(a)
            self.driver.back()
            time.sleep(1)

            # self.driver.swipe(end_x, end_y, start_x, start_y, 1000)
            time.sleep(2)

            print(a)
            return a


        finally:
            pass

    def findEnergy(self):
        self.driver.tap([(1000, 1588)])  # 找能量
        time.sleep(2)

    def yijianshou_zj(self):
        try:
            self.findelement(self.qubaohuTxt0)
            # self.findelement(self.qubaohuTxt1)
        except:
            time.sleep(2)
            self.findelement(self.qubaohuTxt0)
        else:
            self.driver.tap([(530, 1300)])  # 一键收

    def yijianshou_bieren(self):
        time.sleep(5)
        starttime = time.perf_counter()
        i = 0
        try:
            self.findelement(self.nishouquTA4)
            # self.findelement(self.nishouquTA4).text()*********************
            # self.findelement(self.nishouquTA) -****************************看看
        except:
            time.sleep(1)
            print('无能量收取')

        else:
            t = self.findelement(self.nishouquTA4)
            while t:
                # time.sleep(5)
                self.driver.tap([(543, 1388)])  # 一键收
                time.sleep(0.8)
                self.driver.tap([(543, 1388)])  # 一键收
                time.sleep(0.2)
                self.driver.tap([(1000, 1588)])  # 找能量
                time.sleep(5)

                i += 1

                try:
                    t = self.findelement(self.nishouquTA4)
                except:
                    t = False
            endtime = time.perf_counter()
            ii = round((endtime - starttime), 2)
            print('收了{}个人的能量，耗时{}秒'.format(str(i), str(ii)))

        finally:
            t = True
            while t:
                try:
                    self.findelement(self.qubaohuTxt1)

                except:
                    self.driver.back()
                else:
                    t = False

    def todayEnergy2(self):

        try:
            self.click(self.myslIcon)
            time.sleep(5)
            # print(self.driver.page_source)
            self.findelement(self.seeAll)
            a = self.findelement(self.seeAll).text
            print(a)

            self.click(self.seeAll) # qukanquanbu

            time.sleep(2.5)
            # self.driver.refresh()
            print(self.driver.page_source)
            self.findelement(self.todayAll)  # qukanquanbu
            a = self.findelement(self.todayAll).text[5:-1] # qukanquanbu

            print(a)

            a = int(self.findelement(self.todayAll).text[5:-1])  # qukanquanbu

            print(a)
            self.driver.back()
            time.sleep(1.5)

            # self.driver.swipe(end_x, end_y, start_x, start_y, 1000)

            print(a)
            return a

        except:
            pass
        finally:
            pass







# new
# UiSelector().text("text文本")
# # 2.文本比较长的时候，可以用textContains模糊匹配,只要文本包含匹配内容就可以了。
# new
# UiSelector().textContains("包含text文本")
# # 3.textStartsWith是以某个文本开头的匹配
# new
# UiSelector().textStartsWith("以text文本开头")

